package converters;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

import repositories.IncidenciaRepository;
import domain.Incidencia;

@Component
@Transactional
public class StringToIncidenciaConverter implements
		Converter<String, Incidencia> {

	@Autowired
	private IncidenciaRepository incidenciaRepository;

	public Incidencia convert(String text) {
		Incidencia result;
		int id;

		try {

			id = Integer.valueOf(text);
			result = incidenciaRepository.findOne(id);

		} catch (Throwable oops) {
			throw new IllegalArgumentException(oops);
		}

		return result;
	}

}
